/**
 * Copyright: Copyright (c) 2018, 2022
 * Company: 杭州叙简科技股份有限公司
 */
package cn.com.scooper.impl.log;

import cn.com.scooper.common.util.StringUtils;
import cn.com.scooper.dao.log.LogOperateDao;
import cn.com.scooper.dao.log.LogOperateExtDao;
import cn.com.scooper.pojo.log.LogOperateExtPo;
import cn.com.scooper.pojo.log.LogOperatePo;
import cn.com.scooper.pojo.log.LogOperateVo;
import cn.com.scooper.service.log.LogOperateService;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LogOperateServiceImpl implements LogOperateService {
    @Autowired
    private LogOperateDao logOperateDao;
    @Autowired
    private LogOperateExtDao logOperateExtDao;

    private static final Logger logger = LoggerFactory.getLogger(LogOperateServiceImpl.class);

    @Override
    public LogOperatePo getLogOperate(Integer id) {
        return this.logOperateDao.selectByPrimaryKey(id);
    }

    @Override
    public LogOperateVo getLogOperateVo(Integer id) {
        return null;
    }

    @Override
    public int saveLogOperate(LogOperateVo logOperateVo) {
        // 保存日志
        LogOperatePo logOperatePo = logOperateVo.getLogOperatePo();
        int result = this.logOperateDao.insert(logOperateVo.getLogOperatePo());

        if (result > 0) {
            // 保存扩展属性
            Map<String, String> extAttr = logOperateVo.getExtAttr();
            for (Map.Entry<String, String> entry: extAttr.entrySet()) {
                if (StringUtils.notEmpty(entry.getValue())) {
                    LogOperateExtPo logOperateExtPo = new LogOperateExtPo();
                    logOperateExtPo.setLogOperateId(logOperatePo.getId());
                    logOperateExtPo.setExtKey(entry.getKey());
                    logOperateExtPo.setExtValue(entry.getValue());
                    logOperateExtDao.insert(logOperateExtPo);
                }
            }
        }


        return result;
    }

    @Override
    public int removeLogOperate(Integer id) {
        return this.logOperateDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<LogOperatePo> getLogOperateList(LogOperatePo logOperatePo) {
        List<LogOperatePo> list = logOperateDao.selectAll(logOperatePo);
		return list;
    }
}